# -*- coding: UTF-8 -*-
from typing import List

from selenium import webdriver

from bean.hot_new import HotNew
from news_platform.plat.web_platform import WebPlatform

base_url = "https://ishare.ifeng.com/hotNewsRank"
app_path = "pages/new/new?rnum=0&staticId={0}&types=doc&aid={0}&showtype=hotspottitleimg&ref=hot_merge"


class FengHuang(WebPlatform):

    def __init__(self):
        self.name = "凤凰网热榜"

    def query_hot_news(self):
        result: List[HotNew] = []
        chrome_options = webdriver.ChromeOptions()
        chrome_options.add_argument("--headless")
        chrome_options.add_argument("--disable-gpu")
        chrome_options.add_argument("--start-maximized")
        chrome_options.add_argument("--no-sandbox")
        browser = webdriver.Chrome(options=chrome_options)
        try:
            browser.get(base_url)
            browser.implicitly_wait(10)
            all_data = browser.execute_script("return allData")
            new_list = all_data['content']['list']
            serial_no = 0
            for new in new_list:
                hot_new = HotNew(serial_no, new['hotLabel']['eventKeyword'], new['link']['url'],
                                 "5", new['hotLabel']['hotGrade'], new['title'])
                hot_new.app_path = app_path.format(new['id'])
                serial_no = serial_no + 1
                result.append(hot_new)
                # result.append(HotNew(serial_no, title, link, "1", "热搜指数 ", excerpt))

        except Exception as e:
            print(e)
        browser.quit()
        return result

